﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DataAccess.DataObject;
using System.Collections.Generic;

namespace EnmineKaitlin.admin
{
    public partial class poll : Common.PageBase
    {
        const string module = "manage-poll";
        string title = "";
        string lang = "vn";
        string status = "";
        int currentpage = 1;
        int pagesize = 10;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(CheckPermission(module))
            {
            if (!IsPostBack)
            {
                lblFunction.Text = "Thăm dò ý kiến";
                GetParam();
                BindPoll();
            }
            }
            else
            {
                Response.Redirect("~/admin/permission-fail.aspx");
            }
        }
        protected void GetParam()
        {
            if (Request["currentpage"] != null && Request["currentpage"].ToString() != "")
            {
                currentpage = int.Parse(Request["currentpage"].ToString());
            }
            if (Request["s"] != null && Request["s"].ToString() != "")
            {
                title = Request["s"].ToString();
            }
            if (Request["status"] != null && Request["status"].ToString() != "")
            {
                status = Request["status"].ToString();
                ddlStatus.SelectedValue = status;
                ddlStatus.DataBind();
            }
            if (Request["lang"] != null && Request["lang"].ToString() != "")
            {                
                lang = Request["lang"].ToString();
                ddlLanguage.SelectedValue = lang;
                ddlLanguage.DataBind();
            }
        }
        protected void BindPoll()
        {
            IList<OPoll> lstPoll = _BPoll.Get(title, status, "", lang, "CreateDate", "DESC", currentpage, pagesize);
            grvPoll.DataSource = lstPoll;
            grvPoll.DataBind();
            if (lstPoll.Count > 0)
            {
                ctlPagging.PageSize = pagesize;
                ctlPagging.ItemCount = (int)lstPoll[0].TotalResult;
                string url = "lang=" + lang;
                if (title != "") { url += "&s=" + title; }
                if (status != "") { url += "&status=" + status; }
                ctlPagging.QueryStringParameterName = url;
                ctlPagging.CurrentIndex = currentpage;        
            }            
        }
        protected void btnFilter_Command(object sender, CommandEventArgs e)
        {
            if (txtKeyWord.Text != "Nhập từ khóa" && txtKeyWord.Text!="")
            {
                title = txtKeyWord.Text.Trim();
            }
            status = ddlStatus.SelectedValue;
            lang = ddlLanguage.SelectedValue;
            BindPoll();
        }
        protected void lbtFunction_Click(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "BunkFunction")
            {
                bool flag = true;
                string FunctionName = "delete";
                switch (FunctionName)
                {
                    case "delete":
                        foreach (GridViewRow gvr in grvPoll.Rows)
                        {
                            if (((CheckBox)gvr.Cells[0].Controls[0]).Checked)
                            {
                                if (!_BPoll.Delete(int.Parse(grvPoll.DataKeys[gvr.RowIndex].Value.ToString())))
                                {
                                    flag = false;
                                    return;
                                }
                            }
                        }
                        BindPoll();
                        break;
                }
                if (flag == false)
                {
                    lblNotice.Text = "Đã xảy ra lỗi trong quá trình xóa, vui lòng thử lại !";
                }
            }
        }        
        protected void grvPoll_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            AddCheckBoxAll(e);
        }        

        protected void grvPoll_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int PollID = int.Parse(grvPoll.DataKeys[e.RowIndex].Value.ToString());
                if (_BPoll.Delete(PollID))
                {
                    BindPoll();
                }
                else
                {
                    lblNotice.Text = "Đã xảy ra lỗi trong quá trình xóa, vui lòng thử lại !";
                }
            }
            catch (Exception ex) { 
            
            }
        }
    }
}
